package com.xdf.xzymanagementsystem.service.impl;

import com.xdf.xzymanagementsystem.dao.StatusUserMapper;
import com.xdf.xzymanagementsystem.entity.StatusUser;
import com.xdf.xzymanagementsystem.entity.SysUser;
import com.xdf.xzymanagementsystem.service.StatusUserService;
import com.xdf.xzymanagementsystem.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Optional;

@Service
public class StatusUserServiceImpl implements StatusUserService {
    @Autowired
    private StatusUserMapper dao;
    @Autowired
    private SysUserService sysUserService;

    @Override
    public int deleteByPrimaryKey(Integer iid) {
        return dao.deleteByPrimaryKey(iid);
    }

    @Override
    public int insert(StatusUser record) {
        return dao.insert(record);
    }

    @Override
    public int insertSelective(StatusUser record) {
        return dao.insertSelective(record);
    }

    @Override
    public StatusUser selectByPrimaryKey(Integer iid) {
        return dao.selectByPrimaryKey(iid);
    }

    @Override
    public int updateByPrimaryKeySelective(StatusUser record) {
        return dao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(StatusUser record) {
        return dao.updateByPrimaryKey(record);
    }

    @Override
    public StatusUser selectByUserid(String userId) {
        return dao.selectByUserid(userId);
    }

    // 登录成功后才调用本方法，所以 userId 一定存在
    // 注销时调用本方法，所以 userId 一定存在
    // 状态表中不存在该用户则新增，否则修改
    @Override
    public int updateOnlineStatus(String userId,Boolean online) {
        Boolean isNew = false;
        StatusUser statusUser = selectByUserid(userId);
        SysUser sysUser = new SysUser();
        try {
            sysUser = sysUserService.selectByLoginName(userId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("更新用户在线状态时未找到" + userId);
        }
        if( statusUser == null ){
            statusUser = new StatusUser();
            statusUser.setUser_id(userId);
            isNew = true;
        }
        String userName = Optional.ofNullable(sysUser.getUserName()).orElse("");
        statusUser.setUser_name(userName);
        statusUser.setOnline(online);
        statusUser.setOp_time(new Date());


        return isNew == true ? insertSelective(statusUser):updateByPrimaryKeySelective(statusUser);
    }

    @Override
    public int updateOnlineStatus(Integer userPK, Boolean online) {
        Boolean isNew = false;
        SysUser sysUser = new SysUser();
        try {
            sysUser = sysUserService.selectByPrimaryKey(userPK);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("更新用户在线状态时未找到" + userPK);
        }

        String userId = sysUser.getLoginName();
        StatusUser statusUser = selectByUserid(userId);

        if( statusUser == null ){
            statusUser = new StatusUser();
            statusUser.setUser_id(userId);
            isNew = true;
        }
        String userName = Optional.ofNullable(sysUser.getUserName()).orElse("");
        statusUser.setUser_name(userName);
        statusUser.setOnline(online);
        statusUser.setOp_time(new Date());


        return isNew == true ? insertSelective(statusUser):updateByPrimaryKeySelective(statusUser);
    }
}
